﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Helpers;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Modules;
using Vit.Web.Helpers;
using Vit.Web.Providers;

namespace Vit.Web.Mvc.Controllers
{
    public class PageController : Vit.Web.Mvc.Controllers.DataControllerBase<Vit.Web.Models.WebPage>
    {
        [HttpGet]
        public ActionResult Design(string id)
        {
            var page = this.DataProvider.GetByKeys(id);
            TempData["CurrentPageMode"] = PageMode.Design;
            return Redirect(page.SampleUrl);
        }

        [HttpGet]
        public ActionResult Manage(string id)
        {
            var page = this.DataProvider.GetByKeys(id);
            TempData["CurrentPageMode"] = PageMode.Manage;
            return Redirect(page.SampleUrl);
        }

        [HttpGet]
        public ActionResult Preview(string id)
        {
            var page = this.DataProvider.GetByKeys(id);
            TempData["CurrentPageMode"] = PageMode.Visit;
            return Redirect(page.SampleUrl);
        }

        [HttpGet]
        public ActionResult Section(string id, string name)
        {
            var page = this.DataProvider.GetByKeys(name);
            ViewBag.Page = page;
            ViewBag.Section = id;
            return View(page);
        }

        [HttpPost]
        public ActionResult Section(PageWidget widget, FormCollection form)
        {
            var page = this.DataProvider.GetByKeys(widget.WebPageName);
            if (page != null)
            {
                if (string.IsNullOrEmpty(form["EntityID"]))
                {
                    page.Widgets.Add(widget);
                }
                else
                {
                    var dbWidget = page.Widgets.FirstOrDefault(o => o.WebPageName == widget.WebPageName && o.SectionName == widget.SectionName && o.Order == widget.Order);
                    if (dbWidget != null)
                    {
                        dbWidget.WidgetName = widget.WidgetName;
                        dbWidget.Container = widget.Container;
                        dbWidget.CacheSeconds = widget.CacheSeconds;
                        dbWidget.Parameters = widget.Parameters;
                    }
                }
                this.DataProvider.Modify(page);
            }
            return RedirectToAction("Section", new { id = widget.SectionName, name = widget.WebPageName });
        }

        [HttpGet]
        public ActionResult RemoveWidget(string id)
        {
            string[] keys = id.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
            var page = this.DataProvider.GetByKeys(keys[0]);
            var widget = page.Widgets.FirstOrDefault(o => o.SectionName == keys[1] && o.Order.ToString() == keys[2]);
            if (widget != null)
            {
                page.Widgets.Remove(widget);
                this.DataProvider.Modify(page);
            }
            return RedirectToAction("Section", new { id = keys[1], name = keys[0] });
        }

        [HttpGet]
        [Vit.Web.Mvc.Attributes.Widget]
        public ActionResult Header()
        {
            WebSite site = PortalContext.Current.Site;
            if (this.Request.QueryString["pageMode"] == "Manage")
                ViewBag.Edit = true;
            return PartialView("_Header", site);
        }

        [HttpPost]
        public ActionResult Header(WebSite site)
        {
            bool result = PortalContext.Current.Portal.UpdateSite(site);
            PortalContext.Current.Refresh();
            ViewData.Model = site;
            ViewBag.Message = "保存成功";
            return Refresh();
        }

        [Vit.Web.Mvc.Attributes.Widget]
        public ActionResult Footer()
        {
            return PartialView("_Footer");
        }

        [Vit.Web.Mvc.Attributes.Widget("level", "1")]
        public ActionResult Menu(string id)
        {
            int level = 1;
            int.TryParse(id, out level);
            if (!string.IsNullOrWhiteSpace(Request.QueryString["level"]))
                int.TryParse(Request.QueryString["level"], out level);

            var items = MenuHelper.GetMenuItems(level);
            var userItems = items.Where(o => MenuHelper.CanCurrentUserAccessMenuItem(o));
            ViewBag.Level = level;
            return PartialView("_Menu", userItems);
        }

        [Vit.Web.Mvc.Attributes.Widget]
        public ActionResult Crumble()
        {
            var items = MenuHelper.GetCrumblePathItems(null);
            return PartialView("_Crumble");
        }

        [Vit.Web.Mvc.Attributes.Widget]
        public ActionResult Buttons()
        {
            var menu = MenuHelper.GetCurrentMenuItem();
            IEnumerable<NavigationItem> items = menu == null ? Enumerable.Empty<NavigationItem>() : menu.Items;
            var userItems = items.Where(o => MenuHelper.CanCurrentUserAccessMenuItem(o));
            return PartialView("_Buttons", userItems);
        }
    }
}
